<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('绩效人员列表')"/>
    <th:block th:include="include :: layout-latest-css"/>
    <th:block th:include="include :: ztree-css"/>
    <th:block th:include="include :: select2-css"/>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="dept-form">
                <div class="select-list">
                    <ul>
                        <li>
                            二级部门：<select class="form-control" name="deptId" id="deptId" onchange="getTertiaryDept()">
                            <option value="">请选择</option>
                            <option th:each="dep:${secondaryDeptList}"  th:value="${dep.deptId }"
                                    th:text="${dep.deptName }"></option>
                        </select>
                        </li>
                        <li>
                            三级部门：<select class="form-control" name="tertiaryDeptId" id="tertiaryDeptId">
                            <option value="">请选择</option>
                        </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.treeTable.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="toAddTwoDept()">
                <i class="fa fa-plus"></i> 新增二级部门
            </a>
        </div>

        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-tree-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: select2-js"/>
<th:block th:include="include :: layout-latest-js"/>
<th:block th:include="include :: ztree-js"/>
<script th:inline="javascript">

    var prefix = ctx + "acvDepManageController"

    $(function () {
        var options = {
            code: "deptId",
            parentCode: "parentId",
            uniqueId: "deptId",
            url: prefix + "/list",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove/{id}",
            expandAll: false,//是否全部展示
            expandFirst: false,//不展开二级
            modalName: "部门",
            columns: [{
                field: 'selectItem',
                radio: true
            },
                {
                    field: 'deptName',
                    title: '部门名称',
                    align: "left",
                    width: '15%'
                },
                {
                    title: '操作',
                    align: 'left',
                    formatter: function (value, row, index) {
                        if (row.deptType == '绩效二级部门') {
                            var actions = [];
                            var url = ctx + "acvDepManageController/add?deptId=" + row.deptId
                            var title = '新增部门';
                            actions.push('<a class="btn btn-info  btn-xs "href="javascript:void(0)" onclick="$.modal.open(\'' + title + '\',\'' + url + '\',500,500)"><i class="fa fa-plus"></i>新增三级部门</a> ');
                            return actions.join('');
                        } else if(row.deptType == '绩效三级部门'){
                            var actions = [];
                            var url = ctx + "acvDepManageController/edit?deptId=" + row.deptId;
                            var urlAddJob = ctx + "acvDepManageController/addJob?deptId=" + row.deptId;
                            var title = '编辑部门';
                            actions.push('<a class="btn btn-primary  btn-xs "href="javascript:void(0)" onclick="$.modal.open(\'' + '新增岗位' + '\',\'' + urlAddJob + '\',500,500)"><i class="fa fa-plus"></i>新增岗位</a> ');
                            actions.push('<a class="btn btn-success  btn-xs "href="javascript:void(0)" onclick="$.modal.open(\'' + title + '\',\'' + url + '\',500,500)"><i class="fa fa-plus"></i>编辑</a> ');
                            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.deptId + '\')"><i class="fa fa-remove"></i>删除</a> ');
                            return actions.join('');
                        }else if(row.deptType == '岗位'){
                            var actions = [];
                            var url = ctx + "acvDepManageController/editJob?deptId=" + row.deptId;
                            var title = '编辑岗位';
                            actions.push('<a class="btn btn-success  btn-xs "href="javascript:void(0)" onclick="$.modal.open(\'' + title + '\',\'' + url + '\',500,500)"><i class="fa fa-plus"></i>编辑</a> ');
                            actions.push('<a class="btn btn-danger btn-xs" href="javascript:void(0)" onclick="removeJob(\'' + row.deptId + '\')"><i class="fa fa-remove"></i>删除</a> ');
                            return actions.join('');
                        }
                    }
                }
            ]
        };
        $.treeTable.init(options);
    });

    function removeJob(jobId){
        var data = {jobId:jobId};
        $.operate.save(prefix + "/removeJob", data);
    }

    function getTertiaryDept() {
        var deptId = $("#deptId").val();

        var html = "";

        $.ajax({
            async: false,
            url: ctx + "acvEmpManage/getTertiaryDept",
            data: {
                "deptId": deptId
            },
            type: "post",
            dataType: "json",
            success: function (data) {
                $("#tertiaryDeptId").empty();
                var arry = data;
                html += "<option value=''>请选择</option>";
                for (var i = 0; i < arry.length; i++) {
                    html += "<option value='"
                        + arry[i].deptId
                        + "'>"
                        + arry[i].deptName
                        + "</option>";
                }
                $("#tertiaryDeptId").append(html);

            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                alert("错误,请联系管理员,错误信息:" + textStatus);
            }
        });
    }

    function toAddTwoDept() {
        var url = ctx + "acvDepManageController/toAddTwoDept";
        var title = '新增二级部门';
        $.modal.open(title, url, 500, 500);
    }


</script>
</body>
</html>